package com.jbl.te.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.te.entity.College;
import com.jbl.te.entity.Course;
import com.jbl.te.entity.vo.CourseQuery;
import com.jbl.te.entity.vo.CourseVO;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/te/course")
@CrossOrigin
public class CourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    private CollegeService collegeService;

    @GetMapping("/getAllCourse")
    public R getAllCourse() {
        return R.ok().data("list",courseService.list());
    }

    @GetMapping("/getCourseByName")
    public R getCourseByName(String name) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        wrapper.likeRight("name",name);
        return R.ok().data("list",courseService.list(wrapper));
    }

    @GetMapping("/checkCourseByName")
    public R checkCourseByName(String name) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Course course = courseService.getOne(wrapper);
        Boolean b = course!=null;
        if(!b) return R.ok().data("flag",b);
        else return R.ok().data("flag",b).data("id",course.getId());
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("分页课程列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false)CourseQuery courseQuery) {
        Page<Course> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam,courseQuery);
        List<Course> records = pageParam.getRecords();
        List<CourseVO> list = new ArrayList<>();
        for (Course course : records) {
            CourseVO vo = new CourseVO();
            BeanUtils.copyProperties(course,vo);
            College college = collegeService.getById(course.getCollegeId());
            vo.setCollegeName(college.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }
    @DeleteMapping("delCourseById")
    @ApiOperation("删除课程")
    public R delCourseById(String id) {
            //删除功能 TODO
//            return R.error().message("暂不支持删除功能");
        boolean b = courseService.removeById(id);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }
    @DeleteMapping("delCoursesByList")
    @ApiOperation("批量删除课程")
    public R delCoursesByList(@RequestBody  List<String> idList) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = courseService.removeByIds(idList);
//        if(b) {
////            return R.ok();
//        }
//        return R.error().message("批量删除失败");
    }
    @PostMapping("saveCourse")
    @ApiOperation("添加课程(TODO)")
    public R saveCollege(@RequestBody Course course) {

        boolean b = courseService.save(course);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }
    @GetMapping("getCourseById")
    @ApiOperation("根据id查找课程")
    public R getCourseById(String id) {
        Course course = courseService.getById(id);
        if(course == null) {
            return R.error().message("学院不存在");
        }
        CourseVO vo = new CourseVO();
        BeanUtils.copyProperties(course,vo);
        College college = collegeService.getById(course.getCollegeId());
        vo.setCollegeName(college.getName());
        return R.ok().data("course",vo);
    }
    @PutMapping("updateCourse")
    @ApiOperation("修改课程")
    public R updateCourse(@RequestBody Course course) {
        boolean b = courseService.saveOrUpdate(course);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }

}

